﻿/*
Copyright (c) 2010, hiSoft
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the hiSoft nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE HISOFT AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HISOFT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Orchard.Test.Library;
using Selenium;

namespace Orchard.Test.WorkThoughCases.Auto
{
    [TestClass]
    public class DoActionToImportExport
    {
        private TestLibrary TestLibrary;
        private ISelenium selenium;

        [TestInitialize]
        public void SetupTest()
        {
            TestLibrary = TestLibrary.SetupTest(this.ToString());
            selenium = TestLibrary.Selenium;
        }

        [TestCleanup]
        public void TeardownTest()
        {
            TestLibrary.ShutDown();
        }

        [TestMethod]
        public void WorkThoughCases_DoActionToImportExport()
        {
            TestLibrary.UserHelper.LogOnAsAdmin();
            TestLibrary.ModuleHelper.EnableImportExport();
            string dir = "";            
            var widgetLayer=new WidgetLayer("Default");

            #region  +++Page_Published+++

            var pageType = new ContentType("Page", "Page");
            var publishedPage = TestLibrary.PageHelper.CreatePage(new PageSettings() { PublishSetting = PublishSettings.PublishNow });

            TestLibrary.ImportExportHelper.Export(new List<string>() { pageType.TypeId }, false, true, DataExportChoice.Published, false);

            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            TestLibrary.ContentItemHelper.DoActionToContentItem(publishedPage, ContentItemAction.Delete);
            Assert.IsFalse(TestLibrary.ContentItemHelper.IsContentItemPresent(publishedPage));
            TestLibrary.ImportExportHelper.Import(dir);
            Assert.IsTrue(TestLibrary.ContentItemHelper.IsContentItemPresent(publishedPage));
            Assert.AreEqual("Published", TestLibrary.PageHelper.GetPublishStatusOfPage(publishedPage));

            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion


            #region  +++Page_Draft+++
            pageType = new ContentType("Page", "Page");
            var draftPage = TestLibrary.PageHelper.CreatePage(new PageSettings() { PublishSetting = PublishSettings.SaveDraft });
            //clear the export dir
            if (Directory.Exists(TestLibrary.Consts.OrchardAppDataExportsPath))
                Directory.Delete(TestLibrary.Consts.OrchardAppDataExportsPath, true);
            TestLibrary.ImportExportHelper.Export(new List<string>() { pageType.TypeId }, false, true, DataExportChoice.Draft, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            TestLibrary.ContentItemHelper.DoActionToContentItem(draftPage, ContentItemAction.Delete);
            Assert.IsFalse(TestLibrary.ContentItemHelper.IsContentItemPresent(draftPage));

            TestLibrary.ImportExportHelper.Import(dir);
            Assert.IsTrue(TestLibrary.ContentItemHelper.IsContentItemPresent(draftPage));
            Assert.AreEqual("Not Published", TestLibrary.PageHelper.GetPublishStatusOfPage(draftPage));
            Assert.AreEqual("Draft", TestLibrary.PageHelper.GetDraftStatusOfPage(draftPage));

            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion

            #region +++Comment+++

            var contentType = new ContentType("Comment", "Comment");
            TestLibrary.ImportExportHelper.Export(new List<string>() { contentType.TypeId }, true, false, DataExportChoice.Published, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            var contentPartToRemove = new ContentPart("Common");
            TestLibrary.ContentTypeHelper.RemoveContentPartFromContentType(contentType, contentPartToRemove);
            selenium.Open(String.Format("/Admin/ContentTypes/Edit/{0}", contentType.TypeId));
            Assert.IsFalse(selenium.IsTextPresent(contentPartToRemove.Name));

            TestLibrary.ImportExportHelper.Import(dir);
            selenium.Open(String.Format("/Admin/ContentTypes/Edit/{0}", contentType.TypeId));
            Assert.IsTrue(selenium.IsTextPresent(contentPartToRemove.Name));
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion

            #region  +++Blog+++
            var blogType = new ContentType("Blog", "Blog");
            var blog = TestLibrary.BlogHelper.CreateRandomBlog();
            Assert.IsTrue(TestLibrary.BlogHelper.IsBlogPresent(blog));
            TestLibrary.ImportExportHelper.Export(new List<string>() { blogType.TypeId }, false, true, DataExportChoice.Published, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            TestLibrary.BlogHelper.DoActionToBlog(blog, BlogAction.Delete);
            Assert.IsFalse(TestLibrary.BlogHelper.IsBlogPresent(blog));
            TestLibrary.ImportExportHelper.Import(dir);
            TestLibrary.BlogHelper.IsBlogPresent(blog);
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion

            #region +++Post_published+++
            var blogPostType = new ContentType("Blog Post", "BlogPost");
            var publishedPost = TestLibrary.DefaultBlog.CreatePost(new PostSettings() { PublishSetting = PublishSettings.PublishNow });
            TestLibrary.ImportExportHelper.Export(new List<string>() { blogPostType.TypeId }, false, true, DataExportChoice.Published, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            TestLibrary.ImportExportHelper.Import(dir);
            Assert.IsTrue(TestLibrary.DefaultBlog.IsPostPresent(publishedPost));
            Assert.AreEqual("Published", TestLibrary.DefaultBlog.GetPublishStatusOfPost(publishedPost));
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion

            #region +++Post_draft+++
            var draftPost = TestLibrary.DefaultBlog.CreatePost(new PostSettings() { PublishSetting = PublishSettings.SaveDraft });
            TestLibrary.ImportExportHelper.Export(new List<string>() { blogPostType.TypeId }, false, true, DataExportChoice.Draft, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            TestLibrary.ImportExportHelper.Import(dir);
            Assert.IsTrue(TestLibrary.DefaultBlog.IsPostPresent(draftPost));
            Assert.AreEqual("Draft", TestLibrary.DefaultBlog.GetDraftStatusOfPost(draftPost));
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion

            #region +++List+++
            var listType = new ContentType("List", "List");
            var list = TestLibrary.ListHelper.CreateList(new ListSettings());
            Assert.IsTrue(TestLibrary.ContentItemHelper.IsContentItemPresent(list));
            TestLibrary.ImportExportHelper.Export(new List<string>() { listType.TypeId }, false, true, DataExportChoice.Published, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            TestLibrary.ContentItemHelper.DoActionToContentItem(list, ContentItemAction.Delete);
            Assert.IsFalse(TestLibrary.ContentItemHelper.IsContentItemPresent(list));
            TestLibrary.ImportExportHelper.Import(dir);
            Assert.IsTrue(TestLibrary.ContentItemHelper.IsContentItemPresent(list));
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion


            #region +++RecentBlogPostWidget+++
            var recentBlogPostType = new ContentType("Recent Blog Post", "RecentBlogPosts");
            string widgetTitle = String.Format("Widget{0}", UIHelperBase.GenerateUniqueIdentifier(DateTime.Now));
            var widget = TestLibrary.WidgetHelper.AddRecentBlogPosts(widgetLayer, WidgetZone.Header, widgetTitle, 1, TestLibrary.Consts.AdminUserName, TestLibrary.DefaultBlog.Title, "2");
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsTrue(selenium.IsElementPresent("link=" + widget.Title));

            TestLibrary.ImportExportHelper.Export(new List<string>() { recentBlogPostType.TypeId }, false, true, DataExportChoice.Published, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            TestLibrary.WidgetHelper.DeleteWidget(widget);
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsFalse(selenium.IsElementPresent("link=" + widget.Title));
            TestLibrary.ImportExportHelper.Import(dir);
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsTrue(selenium.IsElementPresent("link=" + widget.Title), "Bug#");
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion


            #region +++Html Widget+++
            var htmlWidgetType = new ContentType("Html Widget", "HtmlWidget");
            widget = TestLibrary.WidgetHelper.AddHtmlWidget(widgetLayer, WidgetZone.Header, "htmlWidget001", 1, TestLibrary.Consts.AdminUserName, "test html widget content");
            TestLibrary.ImportExportHelper.Export(new List<string>() { htmlWidgetType.TypeId }, false, true, DataExportChoice.Published, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            Thread.Sleep(6000);
            TestLibrary.WidgetHelper.DeleteWidget(widget);
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsFalse(selenium.IsElementPresent("link=" + widget.Title));
            TestLibrary.ImportExportHelper.Import(dir);
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsTrue(selenium.IsElementPresent("link=" + widget.Title));
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion

            #region +++BlogArchivesWidget+++
            var blogArchivesType = new ContentType("Blog Archives", "BlogArchives");
            widgetTitle = String.Format("Widget{0}", UIHelperBase.GenerateUniqueIdentifier(DateTime.Now));
            widget = TestLibrary.WidgetHelper.AddBlogArchives(widgetLayer, WidgetZone.Header, widgetTitle, 1, TestLibrary.Consts.AdminUserName, TestLibrary.DefaultBlog.Title);
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsTrue(selenium.IsElementPresent("link=" + widget.Title));

            TestLibrary.ImportExportHelper.Export(new List<string>() { blogArchivesType.TypeId }, false, true, DataExportChoice.Published, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            TestLibrary.WidgetHelper.DeleteWidget(widget);
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsFalse(selenium.IsElementPresent("link=" + widget.Title));
            TestLibrary.ImportExportHelper.Import(dir);
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsTrue(selenium.IsElementPresent("link=" + widget.Title), "Bug#");
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion

            #region +++Container Widget+++
            widget = TestLibrary.WidgetHelper.AddContainerWidget(widgetLayer, WidgetZone.Header, "containerWidget001");
            Thread.Sleep(3000);
            TestLibrary.ImportExportHelper.Export(new List<string>() { "ContainerWidget" }, false, true, DataExportChoice.Published, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            Thread.Sleep(6000);
            TestLibrary.WidgetHelper.DeleteWidget(widget);
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsFalse(selenium.IsElementPresent("link=" + widget.Title));
            TestLibrary.ImportExportHelper.Import(dir);

            Assert.IsTrue(selenium.IsTextPresent("Your recipe has been imported"));
            TestLibrary.WidgetHelper.ManageWidgets();
            Assert.IsTrue(selenium.IsElementPresent("link=" + widget.Title), "Bug#");
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion


            #region +++sitesettings+++
            TestLibrary.ImportExportHelper.Export(new List<string>() { "Blog" }, true, true, DataExportChoice.Published, true);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            string originalAllowTypes = TestLibrary.SettingHelper.GetMediaSettingsAllowTypes();
            TestLibrary.SettingHelper.ConfigureMediaSettingsByDeleteSpecialType("jpg");
            TestLibrary.ImportExportHelper.Import(dir);
            Assert.AreEqual(TestLibrary.SettingHelper.GetMediaSettingsAllowTypes(), originalAllowTypes);
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion

            #region +++Layer+++
            var layerType = new ContentType("Widget Layer", "Layer");
            widgetLayer = TestLibrary.WidgetHelper.AddWidgetLayer("newWidgetLayer");
            TestLibrary.ImportExportHelper.Export(new List<string>() { layerType.TypeId }, false, true, DataExportChoice.Published, false);
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            TestLibrary.WidgetHelper.DeleteWidgetLayer(widgetLayer);
            TestLibrary.ImportExportHelper.Import(dir);
            Assert.IsTrue(TestLibrary.WidgetHelper.IsWidgetLayerExist(widgetLayer.Name));
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion

            #region +++newCreatedContentType+++
            ContentField field1 = new ContentField("bookName", FieldType.TextField);
            ContentField field2 = new ContentField("bookPrice", FieldType.TextField);
            ContentPart part1=new ContentPart("Common");
            ContentPart part2 = new ContentPart("Containable");
            var newContentType= TestLibrary.ContentTypeHelper.CreateContentType("newBook", new List<ContentField> {field1,field2 }, new List<ContentPart> { part1,part2});
            TestLibrary.ImportExportHelper.Export(new List<string>() { newContentType.TypeId }, true, true, DataExportChoice.Published, false);
            Assert.IsTrue(newContentType.Parts.Contains(part2));
            TestLibrary.ImportExportHelper.ConfirmExport(ref dir);
            Assert.IsTrue(TestLibrary.ImportExportHelper.isExportOk(dir));
            
            ContentField field3 = new ContentField("bookAuthor", FieldType.TextField);
            var editedType = TestLibrary.ContentTypeHelper.EditContentType(newContentType, "edit" + newContentType.DisplayName, new List<ContentField> { field3 }, null, null, new List<ContentPart> { part2 });
            Assert.IsTrue(TestLibrary.ContentTypeHelper.IsContentTypeExist_byEditLink(editedType));
            
            Assert.IsFalse(editedType.Parts.Contains(part2));
            Assert.IsTrue(editedType.Fields.Contains(field3));

            TestLibrary.ImportExportHelper.Import(dir);
            Assert.IsTrue(editedType.Parts.Contains(part2),"Bug#");
            Assert.IsFalse(editedType.Fields.Contains(field3),"Bug#");
            try { File.Delete(dir); }
            catch (Exception)
            { }
            #endregion
  
        }
    }
}
